package nmsu.ppe.agendabuddy;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.*;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;

public class DayActivity extends Activity
{       
        private Calendar viewDate;
        private DBAdapter dba = new DBAdapter(this);
        private String[][] eventText = new String[10][3];
        private TextView[] dayText = new TextView[10];
        private View eventClicked;
        
        /** Called whenever the activity is created. */
		  
		  
/**		  
* @param savedInstanceState saved current info, uses for activity creation
**/
 
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
                
                // set the view that will be seen
                setContentView(R.layout.day_view);
                
                initDayText( dayText );
                initMenuRegister(); // register the views so they can open the context menu
                
                Bundle extras = getIntent().getExtras();
                viewDate = (Calendar) extras.get("Calendar");
                initDayView( viewDate );

        }
		  
/**
* @param requestCode request for info
* @param resultCode resulting info
* @param data current data
**/
        
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
        {
                initDayView( viewDate );
        }
        
		  
/**
* @param menu menu
* @param v used when event clicked, which view phone is currently in
* @param menuInfo information for the menu
**/ 
		  
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
        {
            super.onCreateContextMenu(menu, v, menuInfo);
            eventClicked = v;
            getMenuInflater().inflate(R.layout.event_context_menu, menu);
        }
        
/**
* @param item used to get item information 
* @return item in context item was selected
**/
		  
        @Override
        public boolean onContextItemSelected(MenuItem item) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            switch (item.getItemId()) {
                case R.id.edit:
                    beginEditEvent();
                    return true;
                case R.id.delete:
                    deleteEvent();
                    return true;
                default:
                    return super.onContextItemSelected(item);
            }
        }
        
        /****** Click events ******/
        
/**
* @param v used to help navigate views
**/
        public void dayForward(View v)
        {
                viewDate.set( Calendar.DAY_OF_MONTH, viewDate.get( Calendar.DAY_OF_MONTH ) + 1 );
                initDayView( viewDate );
        }

        public void dayBackward(View v)
        {
                viewDate.set( Calendar.DAY_OF_MONTH, viewDate.get( Calendar.DAY_OF_MONTH ) - 1 );
                initDayView( viewDate );
        }
        
        public void dayBackClicked(View v)
        {
                Intent intent = new Intent();
                intent.putExtra("Calendar",viewDate);
        setResult(RESULT_OK, intent);
        finish();
        }
        
        public void dayCreateClicked(View v)
        {
                Intent myIntent = new Intent(v.getContext(), NewEventActivity.class);
                myIntent.putExtra("Calendar",viewDate);
                startActivityForResult(myIntent, 0);
        }
        
        public void deleteEvent()
        {
                int i;
                for(i=0; !eventClicked.equals(dayText[i]); i++);
                dba.open();
                dba.deleteEvent(Long.parseLong(eventText[i][2]));
                dba.close();
                initDayView(viewDate);
        }
        
        public void beginEditEvent()
        {       
                // find the event that was clicked
                int i;
                for(i=0; !eventClicked.equals(dayText[i]); i++);
                
                // specify activity to use and what to pass to the activity
                Intent intent = new Intent(eventClicked.getContext(), EditEventActivity.class);
                intent.putExtra("infoArray", eventText[i]);
                intent.putExtra("vd", viewDate);
                startActivityForResult(intent, 0); // start the new activity
        }
        
        /****** Initialize and set date values *******/
/**
* @param c used to set calendar information (year, date, month)
**/
        
		  
		  
        public String initMonth( Calendar c )
        {
                // init month
                TextView monthText = (TextView) findViewById(R.id.month_field);
                int month = c.get(Calendar.MONTH);
                if(monthText != null)
                {
                        switch(month)
                        {
                        case 0:
                                monthText.setText("January");
                                break;
                        case 1:
                                monthText.setText("February");
                                break;
                        case 2:
                                monthText.setText("March");
                                break;
                        case 3:
                                monthText.setText("April");
                                break;
                        case 4:
                                monthText.setText("May");
                                break;
                        case 5:
                                monthText.setText("June");
                                break;
                        case 6:
                                monthText.setText("July");
                                break;
                        case 7:
                                monthText.setText("August");
                                break;
                        case 8:
                                monthText.setText("September");
                                break;
                        case 9:
                                monthText.setText("October");
                                break;
                        case 10:
                                monthText.setText("November");
                                break;
                        case 11:
                                monthText.setText("December");
                                break;
                        default:
                                monthText.setText("Invalid Month");
                                break;
                        }
                }

                return (String) monthText.getText();
        }

        public String initDay()
        {
                TextView dayOfWeek = (TextView) findViewById(R.id.day_field);
                int day = viewDate.get( Calendar.DAY_OF_WEEK );
                switch(day)
                {
                case 1:
                        dayOfWeek.setText("Sunday");
                        break;
                case 2:
                        dayOfWeek.setText("Monday");
                        break;
                case 3:
                        dayOfWeek.setText("Tuesday");
                        break;
                case 4:
                        dayOfWeek.setText("Wednesday");
                        break;
                case 5:
                        dayOfWeek.setText("Thursday");
                        break;
                case 6:
                        dayOfWeek.setText("Friday");
                        break;
                case 7:
                        dayOfWeek.setText("Saturday");
                        break;
                default:
                        dayOfWeek.setText("Error");
                        break;
                }

                return (String) dayOfWeek.getText();
        }
        
        /****** Initalize the day_view ******/
        
        private void initDayView( Calendar c)
        {
                // set the day of week
                initDay(); 

                // set the month field to the month plus the date
                ( (TextView) findViewById( R.id.month_field ) )
                .setText( 
                                initMonth(c) + "  " +
                                                String.valueOf( viewDate.get( Calendar.DATE ) )
                                );

                // get the day's events
                dba.open();
                Cursor cursor = dba.getEvent("" + viewDate.get(Calendar.MONTH) + ":" + 
                                                                                  viewDate.get(Calendar.DATE) + ":" + 
                                                                                  viewDate.get(Calendar.YEAR)           );
                if (cursor.moveToFirst())
                {
                        int i=0;
                        do {
                                eventText[i][0] = cursor.getString(1);
                                eventText[i][1] = cursor.getString(2);
                                eventText[i][2] = cursor.getString(0);
                                if(Integer.parseInt(eventText[i][0].substring(0, 2)) < 12) {
                                        if(Integer.parseInt(eventText[i][0].substring(0, 2)) == 0) {
                                                eventText[i][0] = "12".concat(eventText[i][0].substring(2));
                                        } else if(Integer.parseInt(eventText[i][0].substring(0, 2)) < 10) {
                                                eventText[i][0] = eventText[i][0].substring(1);
                                        }
                                        eventText[i][0] = eventText[i][0].concat(" AM");
                                } else if(Integer.parseInt(eventText[i][0].substring(0, 2)) == 12) { 
                                        eventText[i][0] = eventText[i][0].concat(" PM");
                                } else {
                                        eventText[i][0] = Integer.toString(Integer.parseInt(eventText[i][0].substring(0, 2))-12)
                                                        .concat(eventText[i][0].substring(2).concat(" PM"));
                                }
                                dayText[i].setText( " " + eventText[i][0] + " - " + eventText[i][1]);
                                dayText[i].setVisibility(TextView.VISIBLE);
                                dayText[i].setClickable(true);
                                i++;
                        } while (cursor.moveToNext());

                        for( ; i < 10; i++ )
                        {
                                dayText[i].setVisibility(TextView.INVISIBLE);
                                dayText[i].setClickable(false);
                        }
                } else {
                        dayText[0].setVisibility(TextView.VISIBLE);
                        dayText[0].setClickable(false);
                        dayText[0].setText(" Click New to add events.");
                        for( int i = 1; i < 10; i++ )
                        {
                                dayText[i].setVisibility(TextView.INVISIBLE);
                                dayText[i].setClickable(false);
                        }
                }
                cursor.close();
                dba.close();
                
        }
/**
* @param dayText used to set up our days
**/

        private void initDayText( TextView[] dayText )
        {
                dayText[0] = (TextView)findViewById(R.id.event1);
                dayText[1] = (TextView)findViewById(R.id.event2);
                dayText[2] = (TextView)findViewById(R.id.event3);
                dayText[3] = (TextView)findViewById(R.id.event4);
                dayText[4] = (TextView)findViewById(R.id.event5);
                dayText[5] = (TextView)findViewById(R.id.event6);
                dayText[6] = (TextView)findViewById(R.id.event7);
                dayText[7] = (TextView)findViewById(R.id.event8);
                dayText[8] = (TextView)findViewById(R.id.event9);
                dayText[9] = (TextView)findViewById(R.id.event10);
        }
        
        private void initMenuRegister()
        {
                registerForContextMenu((View) findViewById(R.id.event1));
                registerForContextMenu((View) findViewById(R.id.event2));
                registerForContextMenu((View) findViewById(R.id.event3));
                registerForContextMenu((View) findViewById(R.id.event4));
                registerForContextMenu((View) findViewById(R.id.event5));
                registerForContextMenu((View) findViewById(R.id.event6));
                registerForContextMenu((View) findViewById(R.id.event7));
                registerForContextMenu((View) findViewById(R.id.event8));
                registerForContextMenu((View) findViewById(R.id.event9));
                registerForContextMenu((View) findViewById(R.id.event10));
        }
        
}